<?php
/**
 * User: Kris
 * Date: 2/5/12
 */

if (!function_exists('linkTwitterUsers')) {
    /**
     * Replace @username with a link to that twitter user
     *
     * @param string $text - Tweet text
     * @return string - Tweet text with @replies linked
     */
    function linkTwitterUsers($text)
    {
        $text = preg_replace_callback('/(^|\s)@(\w+)/i', '_linkTwitterUsersCallback', $text);
        return $text;
    }
}

if (!function_exists('_linkTwitterUsersCallback')) {
    function _linkTwitterUsersCallback($matches)
    {
        $linkAttrs = array(
            'class' => 'user'
        );
        return $matches[1] . buildLink('http://twitter.com/' . urlencode($matches[2]), '@' . $matches[2], $linkAttrs);
    }
}

if (!function_exists('linkHashtags')) {

    /**
     * Replace #hashtag with a link to search.twitter.com for that hashtag
     *
     * @param string $text - Tweet text
     * @return string - Tweet text with #hashtags linked
     */
    function linkHashtags($text)
    {
        $text = preg_replace_callback('/(^|\s)(#\w*)/i', '_hashtagLink', $text);
        return $text;
    }
}

if (!function_exists('_hashtagLink')) {
    /**
     * Replace #hashtag with a link to search.twitter.com for that hashtag
     *
     * @param array $matches - Tweet text
     * @return string - Tweet text with #hashtags linked
     */
    function _hashtagLink($matches)
    {
        $linkAttrs = array(
            'class' => 'hashtag'
        );
        return $matches[1] . buildLink('http://search.twitter.com/search?q=' . urlencode($matches[2]), $matches[2], $linkAttrs);
    }
}

if (!function_exists('linkUrls')) {
    /**
     * Turn URLs into links
     *
     * @param string $text - Tweet text
     * @return string - Tweet text with URLs repalced with links
     */
    function linkUrls($text)
    {
        /**
         * match protocol://address/path/file.extension?some=variable&another=asf%
         * $1 is a possible space, this keeps us from linking href="[link]" etc
         * $2 is the whole URL
         * $3 is protocol://
         * $4 is the URL without the protocol://
         * $5 is the URL parameters
         */
        $text = preg_replace_callback("/(^|\s)(([a-zA-Z]+:\/\/)([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9~\/*-?&%]*))/i", '_linkUrlsCallback', $text);

        /**
         * match www.something.domain/path/file.extension?some=variable&another=asf%
         * $1 is a possible space, this keeps us from linking href="[link]" etc
         * $2 is the whole URL that was matched.  The protocol is missing, so we assume http://
         * $3 is www.
         * $4 is the URL matched without the www.
         * $5 is the URL parameters
         */
        $text = preg_replace_callback("/(^|\s)(www\.([a-z][a-z0-9_\..-]*[a-z]{2,6})([a-zA-Z0-9~\/*-?&%]*))/i", '_linkUrlsCallback', $text);

        return $text;
    }
}

if (!function_exists('_linkUrlsCallback')) {
    function _linkUrlsCallback($matches)
    {
        return $matches[1] . buildLink($matches[2],$matches[2]);
    }
}

if (!function_exists('buildLink')) {
    function buildLink($url, $text, $attributes = array())
    {
        return anchor($url, $text, $attributes);
    }
}